package cn.hotel.servlet;

import cn.hotel.pojo.HouseType;
import cn.hotel.service.HouseTypeService;
import cn.hotel.service.HouseTypeServiceImpl;
import cn.hotel.service.MemberService;
import cn.hotel.service.MemberServiceImpl;
import cn.hotel.utlis.MapUtil;
import cn.hotel.utlis.PageBean;
import cn.hotel.utlis.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

@WebServlet(value = {"/houseTypeWork"})
public class HouseTypeServlet extends HttpServlet {
    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        PrintWriter out = resp.getWriter();
        resp.setContentType("text/html;charset=utf-8");
        HouseTypeService houseTypeService = new HouseTypeServiceImpl();
        String type = req.getParameter("type");
        type = StringUtil.isEmpty(type) ? "houseTypeList" : type;
        switch (type){
            //查询
            case "houseTypeList":
                getHousetypeList(houseTypeService,req,resp);
                break;

            case "add":
                //添加
                addHouseType(houseTypeService,req,resp);
                break;

            case "delete":
                //删除
                houseTypeDelete(houseTypeService,req,resp);
                break;
            case "revamp":
                houseTypeRevamp(houseTypeService,req,resp);
                break;


        }
    }

    private void houseTypeRevamp(HouseTypeService houseTypeService, HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter out = resp.getWriter();
        String appPath = req.getContextPath();
        String houseType = req.getParameter("houseType");
        String price = req.getParameter("price");
        String particulars = req.getParameter("particulars");
        int houseNum = 0;
        try {
            houseNum = Integer.parseInt(req.getParameter("houseNum"));
        }catch (Exception e){}
        HouseType houseType1 = new HouseType();
        houseType1.setHouseType(houseType);
        houseType1.setPrice(price);
        houseType1.setParticulars(particulars);
        houseType1.setHouseNum(houseNum);

        Map<String ,Object> revampMap = houseTypeService.revampHouseType(houseType1);
        if ("1".equals(revampMap.get("flag"))){
            out.print("<script>alert('修改成功');location.href='"+appPath+"/houseTypeWork?type=houseTypeList'</script>");
        }else {
            out.print("<script>alert('修改失败');location.href='"+appPath+"/houseTypeWork?type=houseTypeList'</script>");
        }
        out.flush();




    }

    private void houseTypeDelete(HouseTypeService houseTypeService, HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter out = resp.getWriter();
        String deleteHouseType = req.getParameter("houseType");
        String appPath = req.getContextPath();
        Map<String ,Object> deleteMap = houseTypeService.deleteHouseType(deleteHouseType);
        if("1".equals(deleteMap.get("flag"))){
            out.print("<script>alert('删除成功');location.href='"+appPath+"/houseTypeWork?type=houseTypeList'</script>");
        }else {
            out.print("<script>alert('删除失败');location.href='"+appPath+"/houseTypeWork?type=houseTypeList'</script>");
        }
        out.flush();
    }

    private void addHouseType(HouseTypeService houseTypeService, HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter out = resp.getWriter();
        //接收传过来的参数
        String houseType = req.getParameter("houseType");
        String price = req.getParameter("price");
        String particulars = req.getParameter("particulars");
        String appPath = req.getContextPath();


        int houseNum = 0;
        try {
            houseNum = Integer.parseInt(req.getParameter("houseNum"));
        }catch (Exception e){}
        HouseType houseType1 = new HouseType();
        houseType1.setHouseType(houseType);
        houseType1.setPrice(price);
        houseType1.setParticulars(particulars);
        houseType1.setHouseNum(houseNum);

        HouseTypeService houseTypeService1 = new HouseTypeServiceImpl();
        Map<String,Object> resultMap = houseTypeService1.addHouseType(houseType1);
        String flag = MapUtil.getString(resultMap,"flag");
        if ("1".equals(flag)){
            out.print("<script>alert('添加成功');location.href='"+appPath+"/houseTypeWork?type=houseTypeList'</script>");
        }else {
            out.print("<script>alert('添加失败');location.href='"+appPath+"/houseTypeWork?type=houseTypeList'</script>");
        }
        out.flush();
    }

    private void getHousetypeList(HouseTypeService houseTypeService, HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PageBean<HouseType> pageBean = new PageBean<>();
        //接收houseType用来查询
        String houseType = req.getParameter("houseType");
        String current = req.getParameter("current");
        int currentIndex = StringUtil.strToInt(current);
        //计算总条数
        int count = houseTypeService.getCount(houseType);
        //将总条数放到pageBean中
        pageBean.setTotalCount(count);
        //设置当前页
        pageBean.setCurrPageNo(currentIndex);
        List<HouseType> houseTypeList = houseTypeService.getHouseTypeList(houseType,pageBean.getCurrPageNo(),pageBean.getPageSize());
        pageBean.setProductList(houseTypeList);
        req.setAttribute("pageBean",pageBean);
        req.getRequestDispatcher("houseTypejsp/List.jsp").forward(req,resp);

    }

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
